using System.Collections.Generic;
using System.Linq;
using global::GoGameClient.script.global;
using Godot;
using GoGameClient.script.cmd;
using GoGameClient.script.dialog;
using GoGameClient.script.network.webSocket;
using GoGameClient.script.role;

namespace GoGameClient.script.node.select;

public partial class SelectRoleGui : Control
{
    // Called when the node enters the scene tree for the first time.

    private List<RoleSimpleRes> _roleSimpleResList;

    private string _returnScenePath = "res://scene/Caption.tscn";
    private string _createScenePath = "res://scene/CreateRole.tscn";
    private string _worldScenePath = "res://scene/World.tscn";
    private SelectRoleControl _selectRoleControl;
    private Label _name;
    private Label _level;
    private Label _sect;
    private Label _id;
    private TextureRect _background;
    private Model _model;
    private RoleSimpleRes _currentRoleRes;


    public override void _Ready()
    {
        ResourceLoader.LoadThreadedRequest(_returnScenePath);
        ResourceLoader.LoadThreadedRequest(_createScenePath);
        ResourceLoader.LoadThreadedRequest(_worldScenePath);
        _background = GetNode<TextureRect>("RoleBackground");
        _model = GetNode<Model>("RoleControl/Model");
        _id = GetNode<Label>("ID");
        _name = GetNode<Label>("Name");
        _level = GetNode<Label>("Level");
        _sect = GetNode<Label>("Sect");
        _selectRoleControl = GetNode<SelectRoleControl>("SelectRoleControl");
        _selectRoleControl.CurrentRoleChange += OnCurrentRoleChange;
        SocketControl.Instance().Request(new RequestConfig<RoleSimpleResList>
        {
            Title = "获取角色列表",
            Cmd = (int)Router.Role,
            CmdMethod = (int)RoleRouter.FindRoleList,
            Result = res => { _selectRoleControl.SetData(_roleSimpleResList = res.Roles.ToList()); }
        });
    }

    private void OnCurrentRoleChange(long id)
    {
        _currentRoleRes = _roleSimpleResList.Find(item => item.Id == id);
        if (_currentRoleRes == null)
        {
            return;
        }

        _id.Text = _currentRoleRes.Id.ToString();
        _name.Text = _currentRoleRes.Name;
        _level.Text = _currentRoleRes.Level.ToString();
        _sect.Text = _currentRoleRes.Sect;
        _model.SetModel(_currentRoleRes.Model);
        SetBackground(_currentRoleRes.Model);
    }

    private void SetBackground(string model)
    {
        _background.Texture =
            (Texture2D)GD.Load($"res://resources/role/{model}/图片/人物背景.png");
    }


    private void UpButtonPressed()
    {
        if (ResourceLoader.LoadThreadedGetStatus(_returnScenePath) != ResourceLoader.ThreadLoadStatus.Loaded)
        {
            return;
        }

        SocketControl.Instance().Reconnect();
        GetTree().ChangeSceneToPacked((PackedScene)ResourceLoader.LoadThreadedGet(_returnScenePath));
    }

    private void GoWorldButtonPressed()
    {
        if (_currentRoleRes == null)
        {
            TipsDialogUtil.ShowTips(this, "请选择角色");
            return;
        }

        SocketControl.Instance().Request(new BodyRequestConfig<long, RoleRes>()
        {
            Title = "获取角色详情",
            Cmd = (int)Router.Role,
            CmdMethod = (int)RoleRouter.RoleDetail,
            Data = _currentRoleRes.Id,
            Result = res =>
            {
                UserControl.GetInstance().SetRole(res);
                if (ResourceLoader.LoadThreadedGetStatus(_worldScenePath) != ResourceLoader.ThreadLoadStatus.Loaded)
                {
                    return;
                }

                GetTree().ChangeSceneToPacked((PackedScene)ResourceLoader.LoadThreadedGet(_worldScenePath));
            }
        });
    }

    private void CreateButtonPressed()
    {
        if (ResourceLoader.LoadThreadedGetStatus(_createScenePath) != ResourceLoader.ThreadLoadStatus.Loaded)
        {
            return;
        }

        GetTree().ChangeSceneToPacked((PackedScene)ResourceLoader.LoadThreadedGet(_createScenePath));
    }
}